﻿//Kamil.Hawdziejuk@uj.edu.pl
//02.01.2013

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Threading;
using System.ServiceModel;

namespace AppVolume
{
    /// <summary>
    /// Service that changes volume on the system. WCF service uses NAudio library to control that.
    /// </summary>
    [System.AddIn.AddIn("AppVolume")]
    public class AppVolume : Common.ModuleBase
    {
        ServiceHost serviceHost;

        public override void Start()
        {           
            //this.moduleInfo
            //initialize your module here
            //.................instantiate the port
            View.VPortInfo portInfo = GetPortInfoFromPlatform("port");
            var port = InitPort(portInfo);


            //.................register the port after the binding is complete
            RegisterPortWithPlatform(port);


            // make sure that the control never falls out of this function. 
            // o/w, your module will be unloaded then
            // so, if your module doesn’t do anything active but reacts to events from other places
            // use “System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)”  as the last 
            // line of this function


            logger.Log("Started: {0}", ToString());

            AppVolumeSvc volumeService = new AppVolumeSvc(logger);
                
            string homeId = this.GetConfSetting("HomeId");
            string homeIdPart = string.Empty;
            if (!string.IsNullOrEmpty(homeId))
            {
                homeIdPart = "/" + homeId;
            }

            var uri =  new Uri(Globals.InfoServiceAddress + homeIdPart + "/" + moduleInfo.FriendlyName());
            serviceHost = AppVolumeSvc.CreateServiceHost(volumeService,uri);
            serviceHost.Open();

            logger.Log("volume service is open for business");

            //..... get the list of current ports from the platform
            IList<View.VPort> allPortsList = GetAllPortsFromPlatform();

            if (allPortsList != null)
            {
                foreach (View.VPort portItem in allPortsList)
                {
                    PortRegistered(portItem);
                }
            }

        }
    }
}
